/*
 * @Description: 
 * @Author: paddies
 * @Date: 2024-11-15 23:00:34
 * @LastEditors: paddies
 * @LastEditTime: 2025-03-21 11:37:37
 * @FilePath: /shared_components/lib/luntai-components/new-login-panel/newLoginPanel.dart
 */
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:shared_components/build-button/buildButton.dart';
import 'package:shared_components/build-image/buildImage.dart';
import 'package:shared_components/luntai-components/new-login-panel/components/buildBanner.dart';
import 'package:shared_components/luntai-components/new-login-panel/components/buildCommon.dart';
import 'package:shared_components/luntai-components/new-login-panel/controller.dart';

class NewLoginPanel extends StatefulWidget {
  final String requestBaseUrl;
  final Function({String userAccount, String password}) loginAPI;
  final Function({String userAccount, String userType}) rejectAndRegisterAPI;
  final Function loginSuccess;
  final Function({String mobile}) sendRegisterCodeAPI;
  final Function? findPasswordSuccess;
  final Function({String code, String password, String userAccount})
      findPasswordAPI;
  final Function({String mobile}) sendFindCodeAPI;
  final Function registerAPI;
  final String appType;
  const NewLoginPanel(
      {super.key,
      required this.appType,
      required this.sendRegisterCodeAPI,
      required this.requestBaseUrl,
      required this.loginSuccess,
      required this.rejectAndRegisterAPI,
      this.findPasswordSuccess,
      required this.sendFindCodeAPI,
      required this.registerAPI,
      required this.findPasswordAPI,
      required this.loginAPI});

  @override
  State<NewLoginPanel> createState() => _NewLoginPanelState();
}

class _NewLoginPanelState extends State<NewLoginPanel> {
  final NewLoginPanelPageController controller =
      Get.put(NewLoginPanelPageController());
  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    return Scaffold(
        body: Container(
            width: double.infinity,
            height: double.infinity,
            color: Colors.white,
            child: SingleChildScrollView(
              child: Column(children: [
                buildBanner(
                    context: context,
                    solt: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: width / 2,
                          height: width / 2 * 0.2,
                          decoration: const BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage('assets/images/login/logo.png'),
                              fit: BoxFit.fitWidth,
                            ),
                          ),
                        ),
                        const SizedBox(
                          height: 10,
                        ),
                        Container(
                            child: Row(children: [
                          const Text(
                            "还没有账号？",
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 20),
                          ),
                          GestureDetector(
                            onTap: () {
                              controller.openRegister(
                                  context: context,
                                  requestBaseUrl: widget.requestBaseUrl,
                                  appType: widget.appType ?? "user",
                                  registerAPI: widget.registerAPI,
                                  loginSuccess: widget.loginSuccess,
                                  loginAPI: widget.loginAPI,
                                  rejectAndRegisterAPI:
                                      widget.rejectAndRegisterAPI,
                                  sendRegisterCodeAPI:
                                      widget.sendRegisterCodeAPI);
                            },
                            child: Container(
                              decoration: const BoxDecoration(
                                border: Border(
                                  bottom: BorderSide(
                                    color: Color(0xffF9110D),
                                    width: 1.0, // 下边框的宽度
                                  ),
                                ),
                              ),
                              child: const Text(
                                "立即注册",
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 20,
                                    color: Color(0xffF9110D)),
                              ),
                            ),
                          )
                        ]))
                      ],
                    )),
                buildContainer(
                    child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [buildTitle(title: "手机号")],
                )),
                buildContainer(
                    child: CupertinoTextFormFieldRow(
                  padding: const EdgeInsets.only(left: 0, right: 0),
                  style: const TextStyle(fontSize: 18),
                  controller: controller.loginPhoneInputController,
                  inputFormatters: [
                    FilteringTextInputFormatter.digitsOnly,
                    LengthLimitingTextInputFormatter(11), //限制长度
                  ],
                  placeholder: '请输入手机号',
                  placeholderStyle: TextStyle(
                      color:
                          CupertinoColors.darkBackgroundGray.withOpacity(.7)),
                  keyboardType: TextInputType.emailAddress,
                  keyboardAppearance: Brightness.dark,
                  onChanged: (value) {
                    controller.state.loginPhone.value = value.toString();
                  },
                )),
                buildContainer(
                    child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    buildTitle(title: "密码"),
                    GestureDetector(
                      onTap: () {
                        controller.openFindPassword(
                            context: context,
                            appType: widget.appType ?? "user",
                            loginAPI: widget.loginAPI,
                            registerAPI: widget.registerAPI,
                            sendRegisterCodeAPI: widget.sendRegisterCodeAPI,
                            rejectAndRegisterAPI: widget.rejectAndRegisterAPI,
                            loginSuccess: widget.loginSuccess,
                            findPasswordAPI: widget.findPasswordAPI,
                            sendFindCodeAPI: widget.sendFindCodeAPI);
                      },
                      child: buildSmallTitle(title: "忘记密码？"),
                    )
                  ],
                )),
                buildContainer(
                    child: CupertinoTextFormFieldRow(
                  padding: const EdgeInsets.only(left: 0, right: 0),
                  style: const TextStyle(fontSize: 18),
                  obscureText: true,
                  placeholder: '请输入您的密码',
                  placeholderStyle: TextStyle(
                      color:
                          CupertinoColors.darkBackgroundGray.withOpacity(.7)),
                  keyboardType: TextInputType.emailAddress,
                  keyboardAppearance: Brightness.dark,
                  onChanged: (value) {
                    controller.state.loginPassword.value = value.toString();
                  },
                )),
                buildContainer(
                    child: buildButton(
                        title: "立即登录",
                        height: 60,
                        borderRadius: 5,
                        onClick: () {
                          controller.handleLogin(
                              context: context,
                              appType: widget.appType ?? "user",
                              sendRegisterCodeAPI: widget.sendRegisterCodeAPI,
                              registerAPI: widget.registerAPI,
                              rejectAndRegisterAPI: widget.rejectAndRegisterAPI,

                              // submitFn: () {
                              //   widget.loginFn();
                              // },
                              loginAPI: widget.loginAPI,
                              loginSuccess: widget.loginSuccess);
                        },
                        colors: [
                      const Color(0xffe53627),
                      const Color(0xffe53627)
                    ])),
                // Container(
                //   width: double.infinity,
                //   height: 30,
                //   margin: const EdgeInsets.only(top: 30),
                //   child: Row(
                //     children: [
                //       Expanded(
                //           child: SizedBox(
                //               width: double.infinity,
                //               height: double.infinity,
                //               child: Column(
                //                 mainAxisAlignment: MainAxisAlignment.center,
                //                 children: [
                //                   Container(
                //                     width: double.infinity,
                //                     height: 1,
                //                     color:
                //                         const Color.fromARGB(60, 153, 153, 153),
                //                   ),
                //                 ],
                //               ))),
                //       const Padding(
                //         padding: EdgeInsets.only(left: 20, right: 20),
                //         child: Text(
                //           "其他登录方式",
                //           style: TextStyle(color: Color(0xff999999)),
                //         ),
                //       ),
                //       Expanded(
                //           child: SizedBox(
                //         width: double.infinity,
                //         height: double.infinity,
                //         // color: Colors.yellow,
                //         child: Column(
                //           mainAxisAlignment: MainAxisAlignment.center,
                //           children: [
                //             Container(
                //               width: double.infinity,
                //               height: 1,
                //               color: const Color.fromARGB(60, 153, 153, 153),
                //             ),
                //           ],
                //         ),
                //       ))
                //     ],
                //   ),
                // ),
                // Padding(
                //     padding: const EdgeInsets.only(top: 20, bottom: 20),
                //     child: SizedBox(
                //       width: double.infinity,
                //       height: 40,
                //       // color: Colors.red,
                //       child: Row(
                //         children: [
                //           Expanded(
                //               child: Container(
                //             width: double.infinity,
                //             height: double.infinity,
                //             // color: Colors.yellow,
                //             padding: const EdgeInsets.only(right: 30),
                //             alignment: Alignment.centerRight,
                //             child: GestureDetector(
                //                 onTap: () {
                //                   // controller.otherLogion('weixin');
                //                 },
                //                 child: buildImage(
                //                     url: "assets/images/login/weixin.png",
                //                     width: 40,
                //                     height: 40,
                //                     fit: BoxFit.fitHeight)),
                //           )),
                //           Expanded(
                //               child: Container(
                //             width: double.infinity,
                //             height: double.infinity,
                //             // color: Colors.green,
                //             alignment: Alignment.centerLeft,
                //             padding: const EdgeInsets.only(left: 30),
                //             child: GestureDetector(
                //                 onTap: () {
                //                   // controller.otherLogion('qq');
                //                 },
                //                 child: buildImage(
                //                     width: 40,
                //                     height: 40,
                //                     fit: BoxFit.fitHeight,
                //                     url: "assets/images/login/qq.png")),
                //           ))
                //         ],
                //       ),
                //     )),
                buildRadio(),
                const SizedBox(
                  height: 50,
                )
              ]),
            )));
  }

  // 选用户同意书视图
  Widget buildRadio() {
    var privateName = 'private';
    var serviceName = 'service';
    if (widget.appType == 'supplier') {
      privateName = 'private-supply';
      serviceName = 'service-supply';
    }
    if (widget.appType == 'business') {
      privateName = 'private-business';
      serviceName = 'service-business';
    }
    if (widget.appType == 'worker') {
      privateName = 'private-worker';
      serviceName = 'service-worker';
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Expanded(
          flex: 1,
          child: Wrap(
            alignment: WrapAlignment.center,
            crossAxisAlignment: WrapCrossAlignment.center,
            direction: Axis.horizontal,
            children: [
              Obx(() => Checkbox(
                    value: controller.state.isRead.value,
                    onChanged: (v) {
                      controller.changeRead();
                    },
                    activeColor: Colors.black,
                  )),
              const Text("已阅读并同意:"),
              GestureDetector(
                onTap: () {
                  print(
                      "${widget.requestBaseUrl}/download/${privateName}.html");
                  Get.toNamed("/webview", arguments: {
                    "title": "《隐私政策》",
                    "url":
                        "${widget.requestBaseUrl}/download/${privateName}.html"
                  });
                },
                child: const Text(
                  "《隐私政策》",
                  style: TextStyle(color: Color(0xff2C65FF)),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Get.toNamed("/webview", arguments: {
                    "title": "《用户协议》",
                    "url":
                        "${widget.requestBaseUrl}/download/${serviceName}.html"
                  });
                },
                child: const Text(
                  "《用户协议》",
                  style: TextStyle(color: Color(0xff2C65FF)),
                ),
              ),
              Visibility(
                  visible: widget.appType != 'user',
                  child: GestureDetector(
                    onTap: () {
                      Get.toNamed("/webview", arguments: {
                        "title": "《入驻协议》",
                        "url":
                            "${widget.requestBaseUrl}/download/service-join.html"
                      });
                    },
                    child: const Text(
                      "《入驻协议》",
                      style: TextStyle(color: Color(0xff2C65FF)),
                    ),
                  )),
              GestureDetector(
                onTap: () {
                  Get.toNamed("/webview", arguments: {
                    "title": "《开放平台在线服务协议》",
                    "url": "${widget.requestBaseUrl}/download/open-service.html"
                  });
                },
                child: const Text(
                  "《开放平台在线服务协议》",
                  style: TextStyle(color: Color(0xff2C65FF)),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }
}

  // Widget buildBanner() {
  //   var width = MediaQuery.of(context).size.width;
  //   return Container(
  //       width: double.infinity,
  //       height: width * 0.68,
  //       decoration: const BoxDecoration(
  //         image: DecorationImage(
  //           image: AssetImage('assets/images/login/top.png'),
  //           fit: BoxFit.fitWidth,
  //         ),
  //       ),
  //       child: Stack(
  //         children: [
  //           Padding(
  //               padding: const EdgeInsets.only(left: 30),
  //               child: Align(
  //                   alignment: Alignment.centerLeft,
  //                   child: Column(
  //                       mainAxisAlignment: MainAxisAlignment.center,
  //                       crossAxisAlignment: CrossAxisAlignment.start,
  //                       children: [
  //                         const SizedBox(
  //                           height: 30,
  //                         ),
  //                         Container(
  //                           width: width / 2,
  //                           height: width / 2 * 0.2,
  //                           decoration: const BoxDecoration(
  //                             image: DecorationImage(
  //                               image:
  //                                   AssetImage('assets/images/login/logo.png'),
  //                               fit: BoxFit.fitWidth,
  //                             ),
  //                           ),
  //                         ),
  //                         const SizedBox(
  //                           height: 10,
  //                         ),
  //                         Container(
  //                             child: Row(children: [
  //                           const Text(
  //                             "还没有账号？",
  //                             style: TextStyle(
  //                                 fontWeight: FontWeight.bold, fontSize: 20),
  //                           ),
  //                           GestureDetector(
  //                             onTap: () {
  //                               controller.openRegister();
  //                             },
  //                             child: Container(
  //                               decoration: const BoxDecoration(
  //                                 border: Border(
  //                                   bottom: BorderSide(
  //                                     color: Color(0xffF9110D),
  //                                     width: 1.0, // 下边框的宽度
  //                                   ),
  //                                 ),
  //                               ),
  //                               child: const Text(
  //                                 "立即注册",
  //                                 style: TextStyle(
  //                                     fontWeight: FontWeight.bold,
  //                                     fontSize: 20,
  //                                     color: Color(0xffF9110D)),
  //                               ),
  //                             ),
  //                           )
  //                         ]))
  //                       ])))
  //         ],
  //       ));
  // }
// }
